package service

import (
	"encoding/json"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/repository"
	"gitlab.yoyiit.com/youyi/go-core/store"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"golang.org/x/net/context"
)

type BusinessCertificateService interface {
	ListBusinessCertificate(ctx context.Context, req *api.ListBusinessCertificateRequest) (*api.ListBusinessCertificateResponse, error)
	GetBusinessCertificate(ctx context.Context, req *api.BusinessCertificateData) (*api.BusinessCertificateData, error)
	AddBusinessCertificate(ctx context.Context, req *api.BusinessCertificateData) error
	EditBusinessCertificate(ctx context.Context, req *api.BusinessCertificateData) error
	DeleteBusinessCertificate(ctx context.Context, id int64) (err error)
	BatchDeleteBusinessCertificate(ctx context.Context, req *api.BatchDeleteBusinessCertificateRequest) (err error)
}

type somsService struct {
	businessCertificateRepo repo.BusinessCertificateRepo
}

func (s *somsService) ListBusinessCertificate(ctx context.Context, req *api.ListBusinessCertificateRequest) (*api.ListBusinessCertificateResponse, error) {
	dbDatas, count, err := s.businessCertificateRepo.List(ctx, "", req.PageNum, req.PageSize, &repo.BusinessCertificateDBDataParam{
		BusinessCertificateDBData: repo.BusinessCertificateDBData{
			Name:                  req.Data.Name,
			Code:                  req.Data.Code,
			Remark:                req.Data.Remark,
			Attachment:            req.Data.Attachment,
			DueDate:               req.Data.DueDate,
			ImpendingReminderDays: req.Data.ImpendingReminderDays,
			Status:                req.Data.Status,
		},
	})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	var result []*api.BusinessCertificateData
	if dbDatas != nil {
		result = make([]*api.BusinessCertificateData, len(*dbDatas))
		for i, data := range *dbDatas {
			result[i] = &api.BusinessCertificateData{
				Id:                    data.Id,
				OrganizationId:        data.OrganizationId,
				CreatedAt:             util.FormatDateTime(data.CreatedAt),
				UpdatedAt:             util.FormatDateTime(data.UpdatedAt),
				CreatedUserId:         data.CreatedUserId,
				UpdatedUserId:         data.UpdatedUserId,
				UpdatedUserNickname:   data.UpdatedUserNickname,
				CreatedUserNickname:   data.CreatedUserNickname,
				Name:                  data.Name,
				Code:                  data.Code,
				Remark:                data.Remark,
				Attachment:            data.Attachment,
				DueDate:               data.DueDate,
				ImpendingReminderDays: data.ImpendingReminderDays,
				Status:                data.Status,
			}
		}
	}
	return &api.ListBusinessCertificateResponse{
		Data:  result,
		Count: count,
	}, nil
}

func (s *somsService) GetBusinessCertificate(ctx context.Context, req *api.BusinessCertificateData) (*api.BusinessCertificateData, error) {
	data, err := s.businessCertificateRepo.Get(ctx, &repo.BusinessCertificateDBData{
		BaseDBData: repository.BaseDBData{
			BaseCommonDBData: repository.BaseCommonDBData{
				Id: req.Id,
			},
		},
	})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	resp := &api.BusinessCertificateData{
		Id:                    data.Id,
		OrganizationId:        data.OrganizationId,
		CreatedAt:             util.FormatDateTime(data.CreatedAt),
		UpdatedAt:             util.FormatDateTime(data.UpdatedAt),
		CreatedUserId:         data.CreatedUserId,
		UpdatedUserId:         data.UpdatedUserId,
		UpdatedUserNickname:   data.UpdatedUserNickname,
		CreatedUserNickname:   data.CreatedUserNickname,
		Name:                  data.Name,
		Code:                  data.Code,
		Remark:                data.Remark,
		Attachment:            data.Attachment,
		DueDate:               data.DueDate,
		ImpendingReminderDays: data.ImpendingReminderDays,
		Status:                data.Status,
	}
	return resp, nil
}

func (s *somsService) AddBusinessCertificate(ctx context.Context, data *api.BusinessCertificateData) error {
	repoData := &repo.BusinessCertificateDBData{
		Name:                  data.Name,
		Code:                  data.Code,
		Remark:                data.Remark,
		Attachment:            data.Attachment,
		DueDate:               data.DueDate,
		ImpendingReminderDays: data.ImpendingReminderDays,
		Status:                data.Status,
	}
	_, err := s.businessCertificateRepo.Add(ctx, repoData)
	if err != nil {
		return handler.HandleError(err)
	}
	return nil
}

func (s *somsService) EditBusinessCertificate(ctx context.Context, data *api.BusinessCertificateData) error {
	repoData := &repo.BusinessCertificateDBData{
		BaseDBData: repository.BaseDBData{
			BaseCommonDBData: repository.BaseCommonDBData{
				Id: data.Id,
			},
		},
		Name:                  data.Name,
		Code:                  data.Code,
		Remark:                data.Remark,
		Attachment:            data.Attachment,
		DueDate:               data.DueDate,
		ImpendingReminderDays: data.ImpendingReminderDays,
		Status:                data.Status,
	}
	return handler.HandleError(s.businessCertificateRepo.UpdateById(ctx, data.Id, repoData))
}

func (s *somsService) DeleteBusinessCertificate(ctx context.Context, id int64) (err error) {
	return handler.HandleError(s.businessCertificateRepo.DeleteById(ctx, id))
}

func (s *somsService) BatchDeleteBusinessCertificate(ctx context.Context, req *api.BatchDeleteBusinessCertificateRequest) (err error) {
	if req.Ids != nil && len(req.Ids) > 0 {
		// 自行补充逻辑
	}
	return nil
}
